{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9145ba44-c6d3-4ac1-874e-f54e110c15cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from supervoice_valle import Attend\n",
    "from torch.nn.attention import SDPBackend, sdpa_kernel\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c585a532-bf54-4abf-b306-5d820151acd5",
   "metadata": {},
   "outputs": [],
   "source": [
    "a_pt = Attend(engine = \"torch\", heads = 32).to(\"cuda\").eval()\n",
    "a_nt = Attend(engine = \"direct\", heads = 32).to(\"cuda\").eval()\n",
    "a_xt = Attend(engine = \"xformers\", heads = 32).to(\"cuda\").eval()\n",
    "a_ft = Attend(engine = \"flash\", heads = 32).to(\"cuda\").eval()\n",
    "attentions = [a_pt, a_nt, a_xt, a_ft]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bfdeb604-64a0-4f94-9127-9700ba09c9ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Without padding\n",
      "torch 0.0\n",
      "direct 0.00048828125\n",
      "xformers 0.0\n",
      "flash 0.0\n",
      "With padding\n",
      "torch 0.0\n",
      "direct 0.00048828125\n",
      "xformers 0.0\n",
      "flash 0.0\n"
     ]
    }
   ],
   "source": [
    "# Source\n",
    "query = torch.rand(1, 32, 32, 16, dtype=torch.float16, device=\"cuda\")\n",
    "key = torch.rand(1, 32, 32, 16, dtype=torch.float16, device=\"cuda\")\n",
    "value = torch.rand(1, 32, 32, 16, dtype=torch.float16, device=\"cuda\")\n",
    "lengths = [4, 8, 8, 12]\n",
    "\n",
    "print(\"Without padding\")\n",
    "source = a_pt(query, key, value)\n",
    "for a in attentions:\n",
    "    dest = a(query, key, value)\n",
    "    print(a.engine, (dest - source).abs().max().item())\n",
    "\n",
    "print(\"With padding\")\n",
    "source = a_pt(query, key, value, lenghts = lengths)\n",
    "for a in attentions:\n",
    "    dest = a(query, key, value, lenghts = lengths)\n",
    "    print(a.engine, (dest - source).abs().max().item())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "039f7197-074f-4776-a5e7-8c2b5eaabe4b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Without padding\n",
      "torch 4.43998122215271\n",
      "direct 10.454708576202393\n",
      "xformers 8.981621742248535\n",
      "flash 3.2445619106292725\n",
      "With padding\n",
      "torch 16.73130464553833\n",
      "direct 25.77487087249756\n",
      "xformers 16.095849990844727\n",
      "flash 15.358261585235596\n"
     ]
    }
   ],
   "source": [
    "# Benchmark\n",
    "print(\"Without padding\")\n",
    "for a in attentions:\n",
    "    start = time.time()\n",
    "    for i in range(100000):\n",
    "        a(query, key, value)\n",
    "    print(a.engine, time.time() - start)\n",
    "\n",
    "print(\"With padding\")\n",
    "for a in attentions:\n",
    "    start = time.time()\n",
    "    for i in range(100000):\n",
    "        a(query, key, value, lenghts = lengths)\n",
    "    print(a.engine, time.time() - start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05aa22c4-38ca-48e2-8402-f7de96c9be17",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
